<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use yii\grid\GridView;
$this->title = '分类管理';
?>
<div class="container">
	<div class="row">
		<!-- 左侧导航 -->
		<?php echo $this->render('_leftnav') ?>
		<div class="col-md-10">
			<div class="page-header clearfix">
			<h1 class="pull-left">分类管理</h1>
				<div class="pull-right">
					<a class="btn btn-success btn-sm" data-target="#addType" data-toggle="modal" >添加分类</a>
				</div>
			</div>
			<div id="addType" class="modal fade">
				<div class="modal-dialog">
					<div class="modal-content">
						<div class="modal-header">
							<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
							<h4 class="modal-title">添加分类</h4>
						</div>
						<div class="modal-body">
							<div id="type-creater-widget">
								<?php 
                                    $form = ActiveForm::begin([
                                        'options' => ['class' => 'form-horizontal',
										'id'=>'type-form'],
                                        'action'=>'/index.php?r=course/typecreate',
                                        'method'=>'post',
                                        'fieldConfig' => [
                                            'template' => '<div class="form-group"><center><label class="col-md-2 control-label" for="type-name-field">{label}</label></center><div class="col-md-8 controls">{input}{error}</div></div>'
                                        ], 
                                    ]);
                                ?>
                                <?= $form->field($model, 'type_name',['inputOptions'=>['placeholder'=>'请输入分类名称']])->textInput(['maxlength' => true]) ?>

                                <?= $form->field($model, 'type_sort',['inputOptions'=>['value'=>50]])->textInput() ?>

                                <?php $model->is_show=1; echo $form->field($model,'is_show')->radioList(['1'=>'显示','0'=>'不显示'],['separator'=>" ","style"=>"padding-top:6px;"]); ?> 
							</div>
						</div>
						<div class="modal-footer">	
							<button type="button" class="btn btn-link" data-dismiss="modal" tabindex="6">取消</button>
							<?= Html::submitButton('保存', ['class' => 'btn btn-success','id'=>'category-create-btn']) ?>
						</div>
						<?php ActiveForm::end(); ?>
					</div>
				</div>
            </div>
			<div id="updateType" class="modal fade "></div>
            <!-- 分类选项卡 -->
            <?php echo $this->render('_catenav') ?>
			 <table class="table table-hover" id="category-table">
				<thead>
					<tr>
						<th width="30%">名称</th>
						<th width="15%">排序</th>
						<th width="15%">是否显示</th>
						<th width="20%">添加时间</th>
						<th width="20%">操作</th>
					</tr>
				</thead>
				<tbody>
					<?php foreach($data as $k=>$v){?>
					<tr id="category-tr-3">
						<td><?php echo $v['type_name']?></td>
						<td><?php echo $v['type_sort']?></td>
						<td><?php 
								if($v['is_show']==1){
									echo "<font color='green'>显示</font>";
								}else{
									echo "<font color='red'>不显示</font>";
								}	
							?>
						</td>
						<td><?php echo date("Y-m-d H:s:i",$v['type_addtime'])?></td>
						<td>
						<a href="javascript:;" class="btn btn-default btn-sm updateType" data-target="#updateType" data-toggle="modal" type="<?php echo $v['type_id'];?>"><i class="glyphicon glyphicon-edit"></i> 编辑</a>
						<a href="javascript:;" class="btn btn-default btn-sm deletetype" type="<?php echo $v['type_id'];?>"><i class="glyphicon glyphicon-trash"></i> 删除</a>
						</td>
					</tr>
					<?php }?>
				</tbody>
			</table>
		</div>
	</div>
</div>
<script type="text/javascript">
	$(".deletetype").click(function(){
		if(confirm("您确定要删除这条记录？")){
			var type_id=$(this).attr("type");
			$.ajax({
				type: "POST",
				url: "/index.php?r=course/typedelete",
				data: "type_id="+type_id,
				success: function(msg){
					if(msg==1){
						alert("删除成功")
						history.go(0)
					}else{
						alert("请求失败")
					}
				}
			});
		}
	})
	$(".updateType").click(function(){
		var type_id=$(this).attr("type");
		$.ajax({
			type: "POST",
			url: "/index.php?r=course/typeupdate",
			data: "type_id="+type_id+"&type=list",
			dataType: 'json',
			success: function(msg){
				if (msg.code == 200)
				{
					$("#updateType").html(msg.data);
				}else if(msg.code == 403)
				{
					$("#updateType").html('<div class="modal-dialog " style="width:170px;"><div class="modal-content"><div class="modal-header"><button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button><h4 class="modal-title"><font color="red">'+msg.msg+'</font></h4></div></div></div>');
				}
			}
		});
	})
</script>
